const host = "http://cimtech.cn:31143/api"
const host2 = "http://bailitech.cn:9801"
const headers = {
	"x-token": uni.getStorageSync('token')
}
// 航线作业记录
export const getQueryAirlineRecordFlyResult = (id) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: host + `/FlyRt/queryAirlineRecordFlyResult?nestId=${id}`,
			method: "GET",
			header: headers,

			async success(data) {
				// if (data.data.code === 7) {
				// 	await initToken()
				// 	await getQueryAirlineRecordFlyResult()
				// 	console.log(data.data.code)
				// } else {
				resolve({
					code: 200,
					data: data.data
				})
				// }

			},
			fail(err) {
				console.log(err, 'err')
			}

		})
	})
}
// 获取token2
export const getGVAToken = (token) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: host + `/getGvaToken`,
			method: "POST",
			data: {
				tltoken: token
			},
			success(data) {
				resolve({
					code: 200,
					data: data.data
				})
			}
		})
	})
}
// 获取token1
export const getAccessToken = () => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: host2 +
				`/service/magic-api/admin/getToken?loginno=root&password=5a2b5f1c5c520703f4a145aa35a8c32046e9622642ae2f11`,
			method: "GET",
			success(data) {
				resolve({
					code: 200,
					data: data.data
				})
			},
			fail(err) {
				reject({
					code: 400,
					err: err
				})
			}
		})
	})
}
// 初始化token
export const initToken = async () => {
	await getAccessToken().then(res => {
		uni.setStorageSync('accessToken', res.data.accessToken)
	})
	await getGVAToken(uni.getStorageSync('accessToken')).then(res => {
		const {
			data: {
				data: {
					gvatoken
				}
			}
		} = res;
		uni.setStorageSync('token', gvatoken)
	})
	headers['x-token'] = uni.getStorageSync('token')
}
// 航线列表
export const getQryUavAirlineList = () => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: host +
				`/NtAirline/getNestAirlineList?page=1&pageSize=100`,
			method: "GET",
			header: headers,
			async success(data) {
				// if (data.data.code === 7) {
				// 	await initToken()
				// 	await getQueryAirlineRecordFlyResult()
				// } else {
				resolve({
					code: 200,
					data: data.data
				})
				// }

			},
		})
	})
}
// 机巢列表
export const getNestInfoList = () => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: host +
				'/nestinfo/getNestInfoInfoListWithUser',
			method: "GET",
			header: headers,
			async success(data) {
				// if (data.data.code === 7) {
				// 	await initToken()
				// 	await getQueryAirlineRecordFlyResult()
				// } else {
				resolve({
					code: 200,
					data: data.data
				})
				// }

			},
		})
	})
}
// 删除作业记录
export const DeleteFlyResultByIds = (data) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: host +
				'/FlyRt/deleteFlyResultByIds',
			method: "DELETE",
			header: headers,
			data: data,
			async success(data) {
				// if (data.data.code === 7) {
				// 	await initToken()
				// 	await getQueryAirlineRecordFlyResult()
				// } else {
				resolve({
					code: 200,
					data: data.data
				})
				// }

			},
		})
	})
}
// 作业记录下载(目前关闭)
export const postDataResultDownload = (data) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: host +
				'/FlyRt/dataResultDownload',
			method: "POST",
			header: headers,
			data: data,
			responseType: "arraybuffer",
			async success(data) {

				resolve({
					code: 200,
					data: data.data
				})

			},
		})
	})
}
export const stopDownloadingData = async (nestId) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: host +
				`/v1/nest/resetDataloadStatusByNestId?nestId=${nestId}`,
			method: "POST",
			header: headers,
			success(data) {
				resolve({
					code: 200,
					data: data.data
				})

			},
		})
	})
}